<?php
/*
+--------------------------------------------------------------------------
|   phpBIZ v3.0  full version
|   ========================================
|   by taft@wjl.cn yejun@wjl.cn
|   http://www.phpbiz.cn http://www.wjl.cn
|   all rights reserved
+---------------------------------------------------------------------------
|
|   > 最后修改日期：2005-4-30 2006-2-8
|
+--------------------------------------------------------------------------
*/
!class_exists('Debug') && exit('Forbidden');
$s = new Sellercp;
class Sellercp
{
	var $upload_dir;
	var $seller_data;

	
	function Sellercp()
	{
		global $BIZ,$INCOME,$CONF,$_LEVEL,$DB,$cache;

		$this -> upload_dir = stripslashes($CONF['upload_dir']);
		$this -> seller_data = new manager($BIZ->seller['seller_id']);

		//---------------------------------------
		// Kill globals - globals bad, Homer good.
		//---------------------------------------
		
		$tmp_in = array_merge( $_GET, $_POST, $_COOKIE );
		
		foreach ( $tmp_in as $k => $v )
		{
			unset($$k);
		}

    	//--------------------------------------------
    	// Check viewing permissions, etc
		//--------------------------------------------
						
		if ( empty($BIZ->seller['seller_id']) or $BIZ->seller['seller_id'] == "" or $BIZ->seller['seller_id'] == 0 )
		{
			$BIZ->error( "用户没有登陆，不能使用此功能","?act=Login&code=00" );
		}
	    if( $BIZ->seller['seller_check_status']=='uncheck' )
		{
			$this ->  seller_data-> control_panel("nocharge");
		}elseif( ($data=$this -> seller_data->get_seller_data("end_time,property_id"))&& $data['end_time']<time()&&$_LEVEL[$data['property_id']][1]>0 )
		{
			$this ->  seller_data-> control_panel("due");
		}else
		{
			$BIZ -> assign('code',$INCOME['code']);
			$BIZ-> nav_link[] =array ("商家控制台","?act=Seller");
			switch($INCOME['code'])
			{
				case '00':     // display general seller info
				   $this ->  seller_data-> control_panel();
				   break;
				case '01':     //manage shop
				   $this -> shop_form();
   				   $BIZ-> nav_link[] = array("商铺管理","");
				   break;
				case '02':     //
				   $this -> shop_modify_form();
   				   $BIZ-> nav_link[] = array("店面装修","");
				   break;
				case '07':
				   $this -> do_shop_modify();
				   break;
				case '08':
				   $BIZ-> nav_link[] = array("开新商铺","");
				   $this -> new_shop_form();
				   break;
				case '09':
				   $this -> do_add_shop();
    			   break;
				case '10':
				   $this -> del_shop();
				   break;
				case '11':
				   $BIZ-> nav_link[] = array("商品管理","");
				   $this -> goods_form();
				   break;
				case '12':
				   $this -> goods_modify_form();
				   break;
				case '13':
				   $this -> do_goods_modify();
				   break;
				case '14':
				   $this -> do_goods_add();
				   break;
				case '15':
				   $this -> del_goods();
				   break;
				case '16':
					$BIZ-> nav_link[] = array("正在交易商品","");
					$this -> list_all_goods(0);
				   break;
				case '17':
					$BIZ-> nav_link[] = array("回复留言","");
					$this -> message_list();
				   break;
				case '18':
					$this -> reply_form();
				    break;
				case '19':
					$this ->do_message_del();
				  break;
				case '20':
					$this ->do_reply();
				    break;
				case '21':
					$this -> mark_sell();
				  break;
				case '22':
					 $BIZ-> nav_link[] = array("停止交易商品","");
					$this -> list_all_goods(1);
				   break;
				case 'actgoods':
					$BIZ-> nav_link[] = array("等待激活商品","");
					$this -> active_goods_form();
				   break;
				case 'doactive':
					$this -> do_active();
				   break;
				case 'make_html':
					$this -> make_html();
				default:
				  $this ->  seller_data-> control_panel();
			}
		}
		$BIZ -> output('Seller.tpl');
	}
	function shop_modify_form()
	{   // 02 
		global $BIZ,$DB,$INCOME,$CONF;

		$id = intval($INCOME['SHOP_ID']);

		$DB -> db_query( "SELECT s.*,p.pic_name  FROM biz_shop s ,biz_pic p WHERE p.pic_id = s.shop_prime_picture_id  AND s.shop_id = '".$id."' AND s.seller_id = '".$BIZ->seller['seller_id']."'" );
        
		if ( !$DB -> db_fetch_num() ) 
			   $BIZ -> error( "没找到商铺" ,"?act=Seller&code=01");
        $s_re = $DB ->db_fetch_row();
        
		//---------------------------------------------------
		// retrive the property info
		//---------------------------------------------------
		$DB -> db_query( "SELECT a.shop_cat_name as aname,a.shop_cat_id as aid,b.shop_cat_name as bname,b.shop_cat_id as bid FROM biz_shop_cat a LEFT JOIN biz_shop_cat b ON b.shop_cat_parent_id =a.shop_cat_id WHERE a.shop_cat_parent_id = 0" );
		
		$c_r = $DB -> db_fetch_row();
		$pre_id = $c_r['aid'];
		$cat_id[] = $c_r['aid'];
		$cat_name[] = ">>".$c_r['aname']."<<";
		if($c_r['bid'])
		{
			$cat_id[] = $c_r['bid'];
		    $cat_name[] = $c_r['bname'];
		}
		while( $c_r = $DB -> db_fetch_row() )
		{
			if( $pre_id != $c_r['aid'] ) 
			{
				$cat_id[] = $c_r['aid'];
				$cat_name[] = ">>".$c_r['aname']."<<";
				$pre_id =  $c_r['aid'];
			}
			if($c_r['bid'])
			{
				$cat_id[] = $c_r['bid'];
				$cat_name[] = $c_r['bname'];

			}
		}
		$BIZ -> assign("cat_id",$cat_id);
		$BIZ -> assign("cat_name",$cat_name);
        $BIZ -> assign("shop_property_id",$s_re['shop_property_id']);
		//---------------------------------------------------
		
		$BIZ -> assign('pic_path',$BIZ->make_full_path($s_re['pic_name']));
		$BIZ -> assign('ms',$s_re);
		return;
	}

	function do_shop_modify() 
	{   //07
		global $BIZ,$DB,$INCOME,$CONF;

		$su = $INCOME['msd'];

		$DB -> db_query( "SELECT count(*) as ct FROM biz_shop WHERE shop_id='".$INCOME['SHOP_ID']."' AND seller_id ='".$BIZ->seller['seller_id']."'" );
		$res = $DB -> db_fetch_row();
		
		if( $res['ct'] !=1) $BIZ ->error("数据查询错误","?act=Seller&code=01");

		//+------------------------------------------------------------------
		// do pic update stuff if necessay
		//+------------------------------------------------------------------
        
		if( $_FILES['new_pic']['name'] )
		{
			 $prefix='bip_s'.time().mt_rand(1,99999);   // 's' stands for shop
			 $f_upload = new upload_file($prefix,$this -> upload_dir);
             $f_upload->set_file_name_size($_FILES['new_pic']['name'],$_FILES['new_pic']['size']); 
             $f_upload->set_upfile($_FILES['new_pic']['tmp_name']);  
			 $file_name = $f_upload->do_save();
			 if( $INCOME['pic_id'] == 1 )	//1 for absent.gif
			 {
			      $DB -> db_query( " INSERT INTO biz_pic (`pic_shop_id`,`pic_name`) VALUES('$INCOME[SHOP_ID]','$file_name')" );
			      $su['shop_prime_picture_id'] = $DB -> db_insert_id();
			  }
			  else
			  {
			 
			      $DB -> db_query( "SELECT pic_name as n FROM biz_pic WHERE pic_id ='".$INCOME['pic_id']."'");
			      $r = $DB -> db_fetch_row();
			 
			      unlink( $BIZ->make_full_path($r['n']) );
				  unlink( $BIZ->make_full_path($CONF['thumb_prefix'].$r['n'],1) );
             
			      $DB -> db_query( "UPDATE `biz_pic` SET `pic_name` = '{$file_name}' WHERE `pic_id` ='".$INCOME['pic_id']."' LIMIT 1" );
              }

		}
         
		//+------------------------------------------------------------------

		$DB -> db_form_update('biz_shop',$su,"shop_id = '".$INCOME['SHOP_ID']."' AND seller_id ='".$BIZ->seller['seller_id']."'" ,1);
	    $BIZ ->redirect_screen("恭喜！您已经成功完成店面装修","?act=Seller&code=01");	
	}

	function new_shop_form()
	{
		global $BIZ,$DB;

        //check 
        $this -> seller_data ->load_property();
		if( !$this -> seller_data ->check_property("shopt") )
			$BIZ -> error("您已经开设了允许的最大店铺数量。如想开设更多商铺请<a href='index.php?act=User&code=upgrade'>[<font class=red>升级</font>]</a>");

		//---------------------------------------------------
		// retrive the goods property info
		//---------------------------------------------------
		$DB -> db_query( "SELECT a.shop_cat_name as aname,a.shop_cat_id as aid,b.shop_cat_name as bname,b.shop_cat_id as bid FROM biz_shop_cat a LEFT JOIN biz_shop_cat b ON b.shop_cat_parent_id =a.shop_cat_id WHERE a.shop_cat_parent_id = 0" );
		
		$c_r = $DB -> db_fetch_row();
		$pre_id = $c_r['aid'];
		$cat_id[] = $c_r['aid'];
		$cat_name[] = ">>".$c_r['aname']."<<";
		if($c_r['bid'])
		{
			$cat_id[] = $c_r['bid'];
		    $cat_name[] = $c_r['bname'];
		}
		while( $c_r = $DB -> db_fetch_row() )
		{
			if( $pre_id != $c_r['aid'] ) 
			{
				$cat_id[] = $c_r['aid'];
				$cat_name[] = ">>".$c_r['aname']."<<";
				$pre_id =  $c_r['aid'];
			}
			if($c_r['bid'])
			{
				$cat_id[] = $c_r['bid'];
				$cat_name[] = $c_r['bname'];

			}
		}
		$BIZ -> assign("cat_id",$cat_id);
		$BIZ -> assign("cat_name",$cat_name);
		//---------------------------------------------------
	}

	function shop_form()
	{   //01
		global $BIZ,$DB;

		$res = $DB -> db_query( "SELECT * FROM biz_shop WHERE seller_id ='".$BIZ->seller['seller_id']."'" );
        
		if( !$DB -> db_fetch_num() )  
		{
			$BIZ -> assign("none",1);
		}
        
		$stat = array( "checked"=>"已审批","uncheck"=>"<font color=red>未审批</font>");
		$flag = array( "off"=>"未禁止","on"=>"<font color=red>已禁止</font>");
		while( $r = $DB -> db_fetch_row($res) )
		{
			$DB -> db_query( "SELECT count(*) as ct FROM biz_goods WHERE shop_id='".$r['shop_id']."'" );
			$num = $DB -> db_fetch_row();
			$data[]= array( $r[shop_name],$num['ct'],$r[shop_catalog], $stat[$r[shop_check_status]], $flag[$r[shop_ban_status]],  $r[shop_id]);
    	}

	
		$BIZ -> assign('shop_data',$data);
    
		return;

	}

	function do_add_shop()   
	{   //09
		global $BIZ,$INCOME,$DB,$CONF;

		$as = $INCOME['as'];
        $pic_id = 1;  /*default picture*/
		//-------------------------------------------------------
		//first do the upload file stuff if necessary
        //-------------------------------------------------------
	    if($_FILES['up_pic']['name'])	
		{    
			 $prefix='bip_s'.time().mt_rand(1,99999);   // 's' stands for shop
			 $f_upload = new upload_file($prefix,$this -> upload_dir);          
             $f_upload->set_file_name_size($_FILES['up_pic']['name'],$_FILES['up_pic']['size']); 
             $f_upload->set_upfile($_FILES['up_pic']['tmp_name']);  
			 $file_name = $f_upload->do_save();
		
			 $DB -> db_query( "INSERT INTO `biz_pic`"."(`pic_name` ".") VALUES ( '{$file_name}')" );
			 $pic_id = $DB -> db_insert_id();
		}
		
		//-------------------------------------------------------

		$as['seller_id'] = $BIZ ->seller['seller_id'];
	    $as['shop_prime_picture_id '] = $pic_id;
        $as['shop_check_status'] = $CONF['check_shop']?'uncheck':'checked';
		$as['shop_create_time'] =time();
		$DB -> db_form_insert( "biz_shop", $as);
        $shop_id = $DB -> db_insert_id();
		$BIZ -> assign("shop_id",$shop_id);
		if( $pic_id!=1 )$DB -> db_query( " UPDATE biz_pic SET pic_shop_id ='".$shop_id."' WHERE pic_id='".$pic_id."'" );
		$BIZ -> update_repute('shop_add');
		$this -> _update_seller_data("sincrease");   //shop increase
		$BIZ ->redirect_screen("恭喜！您已经成功添加了一个商铺","?act=Seller&code=01");	
	}

	function del_shop()    
	{   //10
		global $INCOME,$DB,$BIZ,$CONF;
          
		$id = intval($INCOME['SHOP_ID']);
   
		$DB -> db_query( "SELECT shop_id FROM biz_shop WHERE shop_id = '".$id."' AND seller_id ='".$BIZ->seller['seller_id']."'" );

		$r = $DB -> db_fetch_row();

		$id = $r['shop_id'];   // for safe reason

		if( !$DB -> db_fetch_num() )  $BIZ ->error("数据查询错误","?act=Seller&code=01");

		//------------------------------------------------------
		//del relative items, don't forget the pic file
		// be care of the absent pic
		//------------------------------------------------------
		$res = $DB -> db_query( " SELECT goods_id as g FROM biz_goods WHERE shop_id = '$id' AND seller_id='".$BIZ->seller['seller_id']."'");
        while( $ar = $DB -> db_fetch_row( $res ) )
		{
			$DB -> db_query( "DELETE FROM biz_message WHERE message_owner_goods_id = '$ar[g]' OR message_owner_shop_id = '$id' " );
			
			//----------del prime good pictures-----------
			$DB -> db_query( "SELECT pic_name as n, pic_id as i FROM biz_pic WHERE pic_goods_id ='".$ar['g']."'");
		    while ($p = $DB -> db_fetch_row())
			{
				if( $p['i'] !=1 )   // jump out of id =0(absent pic)
				{ 
					$pic_file[]= $p['n'];
					$thumb_pic_file[] = $CONF['thumb_prefix'].$p['n'];
			  		$DB -> db_query( "DELETE FROM biz_pic WHERE pic_goods_id = '$ar[g]' " );
				}
			}
			//----------del assistant good pictures
			$DB -> db_query( "SELECT pic_name as n, pic_id as i FROM biz_pic_attach WHERE pic_goods_id ='".$ar['g']."'");
		    while ($p = $DB -> db_fetch_row())
			{
				$pic_file[]= $p['n'];
				$thumb_pic_file[] = $CONF['thumb_prefix'].$p['n'];
			  	$DB -> db_query( "DELETE FROM biz_pic_attach WHERE pic_goods_id = '$ar[g]' " );
			}

		}
		
		$DB -> db_query( "DELETE FROM biz_message WHERE  message_owner_shop_id = '$id' " );
		
		//del shop picture
		$DB -> db_query( "SELECT pic_name as n, pic_id as i FROM biz_pic WHERE pic_shop_id ='$id'" );
		while( $p  = $DB-> db_fetch_row() )
		{
	   			 if( $p['i']!=1 ) 
				 { 
					 $pic_file[] = $p['n']; 
					 $thumb_pic_file[] = $CONF['thumb_prefix'].$p['n'];
				}
		}
    	$BIZ -> unlink_pic( $pic_file );
		$BIZ -> unlink_pic( $thumb_pic_file,1);
		if($id) $DB -> db_query( "DELETE FROM biz_pic WHERE  pic_shop_id ='$id' " );
		//------------------------------------

		$DB -> db_query( "DELETE FROM biz_goods WHERE shop_id = '$id' " );
        $this -> _update_seller_data("gdecrease",$DB->db_affected_rows());
		//------------------------------------------------------
		
		
		$DB -> db_query( "DELETE FROM biz_shop WHERE shop_id = '".$id."' AND seller_id ='".$BIZ->seller['seller_id']."'" );
		$DB -> db_query( "DELETE FROM biz_order_pool WHERE shop_id='".$id."' AND seller_id ='".$BIZ->seller['seller_id']."'");
		$this -> _update_seller_data("sdecrease");
        $BIZ ->redirect_screen("恭喜！您已经成功删除了一个商铺","?act=Seller&code=01");
	}

	function goods_form()   
	{   // 11
		global $BIZ,$INCOME,$DB;

		//check 
        $this -> seller_data ->load_property();
		if( !$this -> seller_data ->check_property("goodst") )
			$BIZ -> error("您已经出售了允许的最大商品数量。如想登陆更多商品请<a href='index.php?act=User&code=upgrade'>[<font class=red>升级</font>]</a>");

		$s_id = intval($INCOME['SHOP_ID']);

		$DB -> db_query( "SELECT * FROM biz_goods WHERE shop_id ='".$s_id."' AND seller_id = '".$BIZ ->seller['seller_id']."' ORDER BY `goods_store_date` DESC " );

		if( ! $DB -> db_fetch_num() )  $BIZ -> assign("msg","商铺里还没有出售商品");
        
		while( $g_re = $DB -> db_fetch_row() )
		{
			$g_data[] = array( $g_re['goods_name'],date( "y-m-d",$g_re['goods_store_date']),$g_re['goods_valid_period'],$g_re['goods_price']."(".$g_re['goods_discount'].")",$g_re['goods_visit_count'],$g_re['goods_mark_sell'],$g_re['goods_id']);
		}

		$BIZ -> assign( "g_data", $g_data );

		//-----------------------------------------------------
		//  read the cat info data
		//-----------------------------------------------------

		$DB -> db_query( " SELECT a.goods_cat_name as aname,a.goods_cat_id as aid,b.goods_cat_name as bname,b.goods_cat_id as bid FROM biz_goods_cat a LEFT JOIN biz_goods_cat b ON b.goods_cat_parent_id =a.goods_cat_id WHERE a.goods_cat_parent_id = 0" );
        
		$c_r = $DB -> db_fetch_row();
		$pre_id = $c_r['aid'];
		$cat_id[] = $c_r['aid'];
		$cat_name[] = $c_r['aname'];
		if($c_r['bid'])
		{
			$cat_id[] = $c_r['bid'];
		    $cat_name[] = "&nbsp;&nbsp;&nbsp;|-".$c_r['bname'];
		}
		while( $c_r = $DB -> db_fetch_row() )
		{
			if( $pre_id != $c_r['aid'] ) 
			{
				$cat_id[] = $c_r['aid'];
				$cat_name[] = $c_r['aname'];
				$pre_id =  $c_r['aid'];
			}
			if($c_r['bid'])
			{
				$cat_id[] = $c_r['bid'];
				$cat_name[] = "&nbsp;&nbsp;&nbsp;|-".$c_r['bname'];

			}
		}
		$BIZ -> assign("cat_id",$cat_id);
		$BIZ -> assign("cat_name",$cat_name);
	
		//------------------------------------------------------

		//------------------------------------------------------
		//read custom catalog info
		//------------------------------------------------------
		$cc_id[] = "0";$cc_title[] ="--不分类--";
		$DB -> db_query("SELECT * FROM biz_custom_catalog WHERE shop_id =".$s_id);
		while( $cc= $DB ->db_fetch_row() )
		{
			$cc_id[]=$cc['id'];
			$cc_title[] =$cc['title'];
		}
		$BIZ -> assign("cc_id",$cc_id);
		$BIZ -> assign("cc_title",$cc_title);			
		//------------------------------------------------------
		
		$BIZ -> assign( "shop_id",$s_id );
		return;
	}

	function do_goods_add() 
	{   // 14
		global $BIZ,$INCOME,$DB;
		$pic_id = 1; /*default picture*/
		//-------------------------------------------------------
		//first do the upload file stuff if necessary
        //-------------------------------------------------------
	    if($_FILES['up_pic']['name'])	
		{    
			 $prefix='bip_g'.time().mt_rand(1,99999);   // 'g' stands for goods
			 $f_upload = new upload_file($prefix,$this -> upload_dir); 
             $f_upload->set_file_name_size($_FILES['up_pic']['name'],$_FILES['up_pic']['size']); 
             $f_upload->set_upfile($_FILES['up_pic']['tmp_name']);  
			 $file_name = $f_upload->do_save();
		
		     $DB -> db_query( "INSERT INTO `biz_pic`"."(`pic_name` ".") VALUES ( '{$file_name}')");
		     $pic_id = $DB -> db_insert_id();
		}
		
		//-------------------------------------------------------
		$ag = $INCOME['ag'];
		$ag['seller_id'] = $BIZ -> seller['seller_id'];  
		$ag['goods_store_date'] = time();
		$to_day = $this -> seller_data -> get_valid_day();    //计算商品失效时间
		$ag['goods_end_active_time'] = time()+ $to_day*3600*24;
		$ag['goods_prime_picture_id'] = $pic_id;
		$DB -> db_form_insert("biz_goods",$ag);
        $goods_id = $DB -> db_insert_id();
		if($pic_id!=1) $DB -> db_query( " UPDATE biz_pic SET pic_goods_id ='".$goods_id."' WHERE pic_id='".$pic_id."'" );

		$BIZ -> update_repute('idx_goods_add');
		$this -> _update_seller_data("gincrease");
   	    $BIZ ->redirect_screen("恭喜！您已经成功上架了一件商品","?act=Seller&code=01");

	}

	function goods_modify_form()  
	{   // 12
		global $DB,$INCOME,$BIZ,$CONF;

		$id = intval($INCOME['GOODS_ID']);

        $DB -> db_query( "SELECT g.*, p.pic_name FROM biz_goods g , biz_pic p WHERE p.pic_id = g.goods_prime_picture_id AND g.goods_id='".$id."' AND g.seller_id='".$BIZ->seller['seller_id']."' LIMIT 1" );

		if( !$DB -> db_fetch_num() ) $BIZ -> error("查询错误");
         
		$g = $DB -> db_fetch_row();
		$BIZ -> assign("pic_path",$BIZ->make_full_path($g['pic_name']));
		$BIZ -> assign( "g",$g );
	    
		//-----------------------------------------------------
		//  read the cat info data
		//-----------------------------------------------------

		$DB -> db_query( " SELECT a.goods_cat_name as aname,a.goods_cat_id as aid,b.goods_cat_name as bname,b.goods_cat_id as bid FROM biz_goods_cat a LEFT JOIN biz_goods_cat b ON b.goods_cat_parent_id =a.goods_cat_id WHERE a.goods_cat_parent_id = 0" );
        
		$c_r = $DB -> db_fetch_row();
		$pre_id = $c_r['aid'];
		$cat_id[] = $c_r['aid'];
		$cat_name[] = $c_r['aname'];
		if($c_r['bid'])
		{
			$cat_id[] = $c_r['bid'];
		    $cat_name[] ="&nbsp;&nbsp;&nbsp;|-".$c_r['bname'];
		}
		while( $c_r = $DB -> db_fetch_row() )
		{
			if( $pre_id != $c_r['aid'] ) 
			{
				$cat_id[] = $c_r['aid'];
				$cat_name[] =$c_r['aname'];
				$pre_id =  $c_r['aid'];
			}
			if($c_r['bid'])
			{
				$cat_id[] = $c_r['bid'];
				$cat_name[] = "&nbsp;&nbsp;&nbsp;|-".$c_r['bname'];

			}
		}
		$BIZ -> assign("cat_id",$cat_id);
		$BIZ -> assign("cat_name",$cat_name);
		$BIZ -> assign("goods_cat_id",$g['goods_cat_id']);
		//------------------------------------------------------
		
		//------------------------------------------------------
		//read custom catalog info
		//------------------------------------------------------
		$DB -> db_query("SELECT shop_id FROM biz_goods WHERE goods_id=".$g[goods_id]);
		$r= $DB -> db_fetch_row();
		$cc_id[] = "0";$cc_title[] ="--不分类--";
		$DB -> db_query("SELECT * FROM biz_custom_catalog WHERE shop_id =".$r[shop_id]);
		while( $cc= $DB ->db_fetch_row() )
		{
			$cc_id[]=$cc['id'];
			$cc_title[] =$cc['title'];
		}
		$BIZ -> assign("cc_id",$cc_id);
		$BIZ -> assign("cc_title",$cc_title);			
		//------------------------------------------------------

		return;
	
	}

	function do_goods_modify()  
	{   // 13
		global $DB,$INCOME,$BIZ,$CONF;

		$mg = $INCOME['mg'];
        
		$id = intval( $INCOME['GOODS_ID'] );

		$mg['shop_id'] = intval( $INCOME['SHOP_ID'] ); 
      
		//+------------------------------------------------------------------
		// do pic update stuff if necessay
		//+------------------------------------------------------------------
        
		if( $_FILES['new_pic']['name'] )
		{
			 $prefix='bip_g'.time().mt_rand(1,99999);   // 's' stands for shop
			 $f_upload = new upload_file($prefix,$this -> upload_dir);          
             $f_upload->set_file_name_size($_FILES['new_pic']['name'],$_FILES['new_pic']['size']); 
             $f_upload->set_upfile($_FILES['new_pic']['tmp_name']);  
			 $file_name = $f_upload->do_save();
			 
			 if( $INCOME['pic_id'] ==1 )
			 {
			       $DB -> db_query( "INSERT INTO biz_pic ( `pic_goods_id`,`pic_name` ) VALUES ( '$id','$file_name' )" );
			       $mg['goods_prime_picture_id'] = $DB -> db_insert_id();
			  }
			  else
			 {			 
			      $DB -> db_query( "SELECT pic_name as n FROM biz_pic WHERE pic_id ='".$INCOME['pic_id']."'");
			      $r = $DB -> db_fetch_row();
			 
			      unlink( $BIZ->make_full_path($r['n']) );
				  unlink( $BIZ->make_full_path($CONF['thumb_prefix'].$r['n'],1) );
             
			      $DB -> db_query( "UPDATE `biz_pic` SET `pic_name` = '{$file_name}' WHERE `pic_id` ='".$INCOME['pic_id']."' LIMIT 1" );
			}

		}
         
		//+------------------------------------------------------------------

		if(!$mg[goods_seller_recommend]) $mg[goods_seller_recommend] = "off";
		$DB -> db_form_update( "biz_goods", $mg ," goods_id ='".$id."' AND seller_id = '".$BIZ -> seller['seller_id']."'");           // including secure option
        
	    $BIZ ->redirect_screen("恭喜！您已经成功修改了商品信息","?act=Seller&code=16");

	}

	function del_goods()
    {   // 15
		global $DB,$INCOME,$BIZ,$CONF;
	
	    $id = intval($INCOME['GOODS_ID']);

		$DB -> db_query( "SELECT goods_id FROM biz_goods WHERE goods_id='".$id."' AND seller_id ='".$BIZ->seller['seller_id']."'" );

		$r= $DB -> db_fetch_row();

		$id = $r['goods_id'];

		if( !$DB->db_fetch_num() ) $BIZ -> error("查询错误","");

        //------------------------------------------------------------
		// Don't forget to delete the pic file
		//------------------------------------------------------------
		$DB -> db_query( "SELECT pic_name as n,pic_id as i FROM biz_pic WHERE pic_goods_id ='".$id."'" );
		
		while( $p = $DB -> db_fetch_row() ) 
			{
				if($p['i']!=1) 
				{
					 $p_cache[]=$p[n]; $thumb_cache[]=$CONF['thumb_prefix'].$p[n];
				}
			}
		
		$BIZ -> unlink_pic(  $p_cache );
		$BIZ -> unlink_pic(  $thumb_cache,1 );
		
       if($id) 
			$DB -> db_query( "DELETE FROM biz_pic WHERE pic_goods_id='".$id."'" );
		
        //------------------------------------------------------------
		// Don't forget to delete the attpic file
		//------------------------------------------------------------
		$DB -> db_query( "SELECT pic_name as nn FROM biz_pic_attach WHERE pic_goods_id ='".$id."'" );
		
		while( $p = $DB -> db_fetch_row() ) 
			{ $p_cache[]=$p[nn]; $thumb_cache[]=$CONF['thumb_prefix'].$p[nn];}
		
		$BIZ -> unlink_pic(  $p_cache );
		$BIZ -> unlink_pic(  $thumb_cache,1 );
		
       if($id) 
			$DB -> db_query( "DELETE FROM biz_pic_attach WHERE pic_goods_id='".$id."'" );
		//------------------------------------------------------------		

		$DB -> db_query( "DELETE FROM biz_message WHERE message_owner_goods_id='".$id."'");
		$DB -> db_query( "DELETE FROM biz_goods WHERE goods_id='".$id."' AND seller_id ='".$BIZ->seller['seller_id']."'");
		$DB -> db_query( "DELETE FROM biz_order_pool WHERE goods_id='".$id."' AND seller_id ='".$BIZ->seller['seller_id']."'");
		
		$this -> _update_seller_data("gdecrease");
		
		$BIZ ->redirect_screen("恭喜！您已经成功删除了一件商品","?act=Seller");	
	}
		
	function list_all_goods($mark)
	{   //16
		global $DB,$BIZ;          //$mark  1 = unsell 

		// find goods which have message 
		$DB -> db_query( "SELECT g.*, count(m.message_content) as ct FROM biz_goods g JOIN biz_message m ON m.message_owner_goods_id = g.goods_id AND m.message_reply_content IS NULL WHERE seller_id = ".$BIZ ->seller['seller_id']." AND goods_mark_sell=".$mark." AND g.goods_end_active_time >".time()." GROUP BY g.goods_id  ");

		while( $g_re = $DB -> db_fetch_row() )
		{
			$g_data[] = array( $g_re['goods_name'],date( "y-m-d H:i",$g_re['goods_store_date']),date("y-m-d H:i",$g_re['goods_end_active_time']),$g_re['goods_price']."(".$g_re['goods_discount'].")",$g_re['goods_visit_count'],$g_re['goods_mark_sell'],$g_re['goods_id'],$g_re['ct'] );
		}
		
		//find goods which not have message
        $DB -> db_query("( SELECT g.* FROM biz_goods g left JOIN biz_message m ON m.message_owner_goods_id = g.goods_id  WHERE g.seller_id = ".$BIZ ->seller['seller_id']." AND goods_mark_sell=".$mark." AND g.goods_end_active_time >".time()." AND m.message_content IS NULL ) UNION  (SELECT g.* FROM biz_goods g left JOIN biz_message m ON m.message_owner_goods_id = g.goods_id  WHERE g.seller_id = ".$BIZ ->seller['seller_id']." AND goods_mark_sell=".$mark." AND g.goods_end_active_time >".time()." AND m.message_content IS NOT NULL AND m.message_reply_content IS NOT NULL) ");
		
		while( $g_re = $DB -> db_fetch_row() )
		{
			$g_data[] = array( $g_re['goods_name'],date( "y-m-d H:i",$g_re['goods_store_date']),date("y-m-d H:i",$g_re['goods_end_active_time']),$g_re['goods_price']."(".$g_re['goods_discount'].")",$g_re['goods_visit_count'],$g_re['goods_mark_sell'],$g_re['goods_id'],$g_re['ct'] );
		}
		
		if( !count($g_data) ) 
		$BIZ-> assign("msg","提示：您还未登陆商品或还未有该类商品。请开新店后添加或在原店里增加商品");	
		//$BIZ -> error("","");
		$BIZ -> assign( "g_data", $g_data );
	    
	}
	function message_list()
	{//17
		global $DB,$BIZ,$INCOME;

		$id = intval($INCOME['GOODS_ID']);

		$DB -> db_query( "SELECT * FROM biz_message WHERE message_owner_goods_id ='".$id."' ORDER BY `message_write_date` DESC " );

		if( !$DB -> db_fetch_num() )
			   $BIZ -> error("该商品未有留言","?act=Seller&code=16");

        while( $r = $DB -> db_fetch_row() )
		{
			$reply_flag = $r['message_reply_content']?1:0;
			$tm = date("Y-m-d H:i",$r['message_write_date']);
			$message[] = array($r['message_writer_title'],$r['message_content'],$tm,$r['message_id']."|".$id,$reply_flag);
		}
		$BIZ -> assign("msg",$message);
	}
	function reply_form()
	{//18
		global $DB,$BIZ,$INCOME;

		$id = explode("|",$INCOME['MESSAGE_ID']);

		$DB -> db_query( "SELECT * FROM biz_message WHERE message_id ='".$id[0]."' AND message_owner_goods_id ='".$id[1]."'" );
		
		if( !$DB -> db_fetch_num() )
			 $BIZ -> error("该记录为空","?act=Seller&code=16");
		
		$m = $DB -> db_fetch_row();
		
		$BIZ -> assign("m",$m);

	}

	function do_message_del()
	{//19
	    global $DB,$BIZ,$INCOME;
		
        $id = explode("|",$INCOME['MESSAGE_ID']);
		
		$DB -> db_query("SELECT * FROM biz_message WHERE message_id ='".$id[0]."'");
		
		if( !$DB -> db_fetch_num() )
			 $BIZ -> error("该记录为空","?act=Seller&code=16");
		$DB -> db_query("DELETE FROM biz_message WHERE message_id ='".$id[0]."' AND message_owner_goods_id ='".$id[1]."'");
		$BIZ ->redirect_screen("恭喜！您已经成功删除了一条留言","?act=Seller");	
	}
	function do_reply()
	{//20
	     global $DB,$BIZ,$INCOME;

		 $gid = intval($INCOME['GOODS_ID']);
		 $id = intval($INCOME['MESSAGE_ID']);

		 $DB -> db_query( "SELECT * FROM biz_message WHERE message_id ='".$id."' AND message_owner_goods_id ='".$gid."'" );
		 if( !$DB -> db_fetch_num() )
			 $BIZ -> error("该记录为空","?act=Seller&code=16");

		 $DB -> db_query( "UPDATE `biz_message` SET `message_reply_content` = '".$INCOME['message_reply_content']."' WHERE `message_id` ='".$id."' AND message_owner_goods_id ='".$gid."'" );

	}
	
	function mark_sell()
	{//21
	      global $DB,$BIZ,$INCOME;
		  $gid = intval($INCOME['GOODS_ID']);
		  
		  if($gid) 
			  $DB -> db_query( "SELECT * FROM biz_goods WHERE goods_id='".$gid."' AND seller_id ='".$BIZ->seller['seller_id']."'");
	
		  if( !$DB -> db_fetch_num() )
			  $BIZ -> error("该记录为空","?act=Seller&code=16");

		  $r  = $DB -> db_fetch_row();
		  $marked = !$r['goods_mark_sell'];		  
		  
		  $DB -> db_query(" UPDATE biz_goods SET goods_mark_sell = '".$marked."' WHERE goods_id='".$gid."'");
	
	      $BIZ ->redirect_screen("恭喜！您已经成功标记了一件商品","?act=Seller");	

		  return;
	}

	function _update_seller_data($action,$row=1)
	{
		global $DB,$BIZ;
		switch($action)
		{
			case 'sincrease':
				$DB -> db_query("UPDATE biz_seller_data SET shop_total = shop_total + $row WHERE seller_id =".$BIZ->seller['seller_id']);
				break;
			case 'sdecrease':
				$DB -> db_query("UPDATE biz_seller_data SET shop_total = shop_total - $row WHERE seller_id =".$BIZ->seller['seller_id']);
				break;
			case 'gincrease':
				$DB -> db_query("UPDATE biz_seller_data SET goods_total = goods_total + $row WHERE seller_id=".$BIZ->seller['seller_id']);
				break;
			case 'gdecrease':
				$DB -> db_query("UPDATE biz_seller_data SET goods_total = goods_total - $row WHERE seller_id=".$BIZ->seller['seller_id']);
				break;
		}
	}
	function active_goods_form()
	{
		global $INCOME,$BIZ,$DB;
		
		$DB -> db_query( "SELECT * FROM biz_goods  WHERE seller_id = ".$BIZ ->seller['seller_id']." AND goods_end_active_time <".time() );

		while( $g_re = $DB -> db_fetch_row() )
		{
			$g_data[] = array( $g_re['goods_name'],date( "y-m-d H:i",$g_re['goods_store_date']),date("y-m-d H:i",$g_re['goods_end_active_time']),$g_re['goods_price']."(".$g_re['goods_discount'].")",$g_re['goods_visit_count'],$g_re['goods_mark_sell'],$g_re['goods_id'],$g_re['ct'] );
		}
		if( !count($g_data) ) 
		$BIZ-> assign("msg","提示：您还未登陆商品或还未有该类商品。请开新店后添加或在原店里增加商品");	
		$BIZ -> assign( "g_data", $g_data );

	}
	function do_active()
	{
		global $INCOME,$BIZ,$DB;
		
		$to_day = $this -> seller_data -> get_valid_day();    //计算商品失效时间
		$endtime = time()+ $to_day*3600*24;
        $DB -> db_query("UPDATE biz_goods SET goods_end_active_time =".$endtime.",goods_store_date='".time()."' WHERE goods_id=".intval($INCOME['GOODS_ID']));
		$BIZ ->redirect_screen("恭喜！您已经成功地激活了一件商品。","?act=Seller&code=16");
        return;
	}
	function make_html()
	{
		global $INCOME,$BIZ;
		if(count($INCOME['html_id'])==0)
			$BIZ -> error("你未钩选要生成的静态商铺！");
		foreach($INCOME['html_id'] as $v )
		{
			if($v) $this -> shop_html($v);
		}
			$BIZ -> redirect_screen("恭喜！您已经成功地生成了静态页面。","?act=Seller&code=01");
	}
	function shop_html($shop_id)
	{
		global $cache,$CONF;
		$handle = fopen($CONF['biz_url']."index.php?act=sd&SHOP_ID=".$shop_id, "rb");
		$contents = '';
		while (!feof($handle)) {
		$contents .= fread($handle, 8192);
		}
		fclose($handle);
		$contents = str_replace("upload/",$CONF['biz_url']."upload/",$contents);
		$contents = str_replace("index.php",$CONF['biz_url']."index.php",$contents);
		$cache -> write_cache('html/shop'.$shop_id.'.html',$contents);
	}
}
?>
